<?php
/**
 *
 * @author zhuzhenguo
 * @version 1.0
 *
 * 用户帐户数据层model
 * 
 */
class ModelAccountAccount extends Model {
	public function getBase($account_id) {
		$query = $this->db->query("SELECT 
				ID AS id, 
				Alias AS alias,
				Email AS email, 
				Gender AS gender, 
				Birthday AS birthday, 
				MobilePhone AS mobilePhone, 
				BirthdayVisible AS birthdayVisible,
				Description AS description, 
				Constellation AS constellation, 
				ConstellationVisible AS constellationVisible, 
				BloodType AS bloodType, 
				BloodTypeVisible AS bloodTypeVisible, 
				PresentPlaceCountryID AS presentPlaceCountryID, 
				PresentPlaceProvinceID AS presentPlaceProvinceID, 
				PresentPlaceCityID AS presentPlaceCityID, 
				PresentPlaceDistrictID AS presentPlaceDistrictID, 
				PresentVisible AS presentVisible, 
				HomePlaceCountryID AS homePlaceCountryID, 
				HomePlaceProvinceID AS homePlaceProvinceID, 
				HomePlaceCityID AS homePlaceCityID, 
				HomePlaceDistrictID AS homePlaceDistrictID, 
				HomeVisible AS homeVisible, 
				MobilePhoneVisible AS mobilePhoneVisible,
				TelephoneVisible AS telephoneVisible,
				AddressVisible AS addressVisible,
				ZipCodeVisible AS zipCodeVisible,
				ContactEmailVisible AS contactEmailVisible,
				IMAccountVisible AS IMAccountVisible,
				Description AS description, 
				Address AS address,
				LastDatetime AS lastDatetime, 
				Telephone AS telephone,
				ContactEmail AS contactEmail,
				AvatarID AS avatarID, 
				ZipCode AS zipCode, 
				IMAccount AS IMAccount, 
				LastIP AS lastIP FROM " . DB_PREFIX . "account WHERE ID='" . (int)$account_id . "'");
		
		return $query->row;
	}
	/**
	 * 查找简单用户信息
	 *
	 * @param array $data (
	 * string email: 邮箱,
	 * string alias: 昵称,
	 * int gender: 性别[0:空, -1:男, 1:女],
	 * string birthday: 生日[yyyy-mm-dd],
	 * int constellation: 星座[0:空, 1:白羊, 2:金牛, 3:双子, 4:巨蟹, 5:狮子, 6:处女, 7:天枰, 8:天蝎, 9:射手, 10:魔蝎, 11:水瓶, 12:双鱼],
	 * int bloodType: 血型[0:空, 1:A型, 2:B型, 3:AB型, 4:O型, 5:其他],
	 * array idList: 用户id列表,
	 * array aliasList: 用户昵称列表,
	 * int status: 帐号状态,
	 * string sort 排序字段,
	 * string order 排序顺序['DESC','ASC']
	 * )
	 * @return array 用户信息
	 *
	 * @author zhuzhenguo
	 * Modified: 2012/08/24 11:20
	 * 1.添加aliasList参数用户替换say.php中出现的getAccountIDByAlias()；2.添加排序；3.合并searchSimple()和getSimple()
	 * @author zhuzhenguo
	 * Modified: 2012/12/10 00:27
	 * 1.更改函数名 searchSimple 为 getSimpleList, 2.增加getAvatar参数未设置时不查询头像, 3.增加email查询结果项, 4.增加QQOpenid查询参数 , 5. 添加状态查询参数
	 */
	public function getSimpleList($data) {
		$sql = $implodeJoin = $select = '';
		$implode = array();
		
		if ( isset($data['email']) ) {
			$implode[] = "A.Email='" . $this->db->escape($data['email']) . "'";
		}
		if ( isset($data['alias']) ) {
			$implode[] = "A.Alias='" . $this->db->escape($data['alias']) . "'";
		}
		if ( isset($data['gender']) ) {
			$implode[] = "A.Gender='" . (int)$data['gender'] . "'";
		}
		if ( isset($data['birthday']) ) {
			$implode[] = "A.Birthday='" . $this->db->escape($data['birthday']) . "'";
		}
		if  (isset($data['constellation']) ) {
			$implode[] = "A.Constellation='" . (int)$data['constellation'] . "'";
		}
		if ( isset($data['bloodType']) ) {
			$implode[] = "A.BloodType='" . (int)$data['bloodType'] . "'";
		}
		if ( isset($data['qqOpenid']) ) {
			$implode[] = "A.QQOpenid='" . $this->db->escape($data['qqOpenid']) . "'";
		}
		if ( isset($data['status']) ) {
			$implode[] = "A.Status='" . (int)$data['status'] . "'";
		}
		if ( isset($data['idList']) ) {
			if (is_array($data['idList'])) {
				$implode[] = "A.ID in(" . implode(',', $data['idList']) . ")";
			}
			else {
				return array();
			}
		}
		
		if ( isset($data['getAvatar']) ) {
			$implodeJoin = " LEFT JOIN " . DB_PREFIX . "account_avatar AS AA ON A.AvatarID=AA.ID";
			$select = ", A.AvatarID AS avatarID, AA.Domain AS domain, AA.Directory AS directory, AA.Filename AS filename";
		}
		
		if ($implode) {
			$sql .= " WHERE " . implode(" AND ", $implode);
		}
		
		if ( isset($data['sort']) ) {
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$data['order'] = " DESC";
			} else {
				$data['order'] = " ASC";
			}
				
			switch ($data['sort']) {
				case 'id':
					$sql .= " ORDER BY A.ID" . $data['order'];
					break;
				case 'alias':
					$sql .= " ORDER BY A.Alias" . $data['order'];
					break;
			}
		}
		
		$query = $this->db->query("SELECT A.ID AS id, A.Email AS email, A.Alias AS alias, A.Gender AS gender, A.Description AS description" . $select . " FROM " . DB_PREFIX . "account AS A" . $implodeJoin . $sql);
		
		return $query->rows;
	}
	/**
	 * 检查当前用户密码是否正确
	 * @param string $passwd 密码
	 * 
	 * @return int 是否正确[0.错误, >0.正确]
	 * 
	 * @author zhuzhenguo 2012/12/10 00:07
	 * 1.添加注释
	 */
	public function checkPassword($passwd) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "account WHERE ID='" . (int)$this->member->getId() . "' AND Password='" . $this->db->escape($passwd) . "'");
		
		return $query->row['total'];
	}
	
	public function getSimple($account_id) {
		$query = $this->db->query("SELECT 
				A.ID AS id, 
				A.Alias AS alias,
			    A.Gender AS gender,
				A.Description AS description, 
				A.Telephone AS telephone, 
				A.AvatarID AS avatarID, 
				AA.Domain AS domain, 
				A.PeopleType AS peopleType,
				AA.Directory AS directory, 
				AA.Filename AS filename FROM " . DB_PREFIX . "account AS A LEFT JOIN " . DB_PREFIX . "account_avatar AS AA ON A.AvatarID=AA.ID WHERE A.ID='" . (int)$account_id . "'");
		
		return $query->row;
	}
	
	public function getScore($account_id) {
		$query = $this->db->query("SELECT Score AS score, ActivityBad AS activityBad, ActivityGood AS activityGood, SponsorBad AS sponsorBad, SponsorGood AS sponsorGood FROM " . DB_PREFIX . "account WHERE ID='" . (int)$account_id . "'");
		
		return $query->row;
	}
	
	public function getBalance($account_id) {
		$query = $this->db->query("SELECT Balance AS balance FROM " . DB_PREFIX . "account WHERE ID='" . (int)$account_id . "'");
		
		return $query->row;
	}
	
	public function getAvatar($avatar_id) {
		$query = $this->db->query("SELECT Domain AS domain, Directory AS direcotory, Filename AS filename FROM " . DB_PREFIX . "account_avatar WHERE ID='" . (int)$avatar_id . "'");
		
		return $query->row;
	}
	
	public function getContact($account_id) {
		$query = $this->db->query("SELECT AddressCountryID AS addreessCountry, AddressProvinceID AS addressProvince, AddressCityID AS addressCity, AddressDistrictID AS addressDistrict, Address AS address, AddressVisible AS addressVisible, ZipCode AS zipCode, ZipCodeVisible AS zipCodeVisible, ContactEmail AS contactEmail, ContactEmailVisible AS contactEmailVisible, IMAccount AS imAccount, IMAccountVisible AS imAccountVisible, MobilePhone AS mobilePhone, MobilePhoneVisible AS mobilePhoneVisible, Telephone AS telephone, TelephoneVisible AS telephoneVisible FROM " . DB_PREFIX . "account WHERE ID='" . (int)$account_id . "'");
		
		return $query->row;
	}
	/**
	 * 添加帐户
	 * @param array $data (
	 * string email: 邮箱,
	 * string password: 密码,
	 * string alias: 昵称,
	 * int gender: 性别[<1. 女，0. 未设置，>1. 男],
	 * string birthday: 生日,
	 * string birthdayVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * int constellation: 星座[0.未设置，1.白羊座，2.金牛座，3.双子座，4.巨蟹座，5.狮子座，6.处女座，7.天枰座，8.天蝎座，9.射手座，10.魔蝎座，11.水瓶座，12.双鱼座],
	 * string constellationVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * int bloodType: 血型：[0.未设置，1.A型，2.B型，3.AB型，4.O型，5.其他],
	 * string bloodTypeVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * int presentPlaceCountry: 现居地的国家,
	 * int presentPlaceProvince: 现居地的省,
	 * int presentPlaceCity: 现居地的城市,
	 * int presentPlaceDistrict: 现居地的地区,
	 * string presentPlaceVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * int homePlaceCountry: 家乡的国家,
	 * int homePlaceProvince: 家乡的省,
	 * int homePlaceCity: 家乡的城市,
	 * int homePlaceDistrict: 家乡的地区,
	 * string homePlaceVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * string description: 个人简介,
	 * string mobilePhone: 移动电话,
	 * string mobilePhoneVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * string telephone: 电话,
	 * string telephoneVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * int addressCountry: 联系方式的国家,
	 * int addressProvince: 联系方式的省,
	 * int addressCity: 联系方式的城市,
	 * int addressDistrict: 联系方式的地区,
	 * string address: 联系方式的具体地址,
	 * string addressVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * string zipCode: 邮编,
	 * string zipCodeVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * string contactEmail: 联系邮箱,
	 * string contactEmailVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * int avatarID: 头像ID,
	 * string imAccount: 即时通讯帐号[json],
	 * string imAccountVisible: 可见范围 [A.所有可见，M.自己，F.好友，L.关注你的人，C.你关注的人],
	 * int status: 帐户状态：[1.正常，0.停用，2.待验证],
	 * string qqOpenid: QQ OpenID,
	 * string qqAccessToken: QQ AccessToken,
	 * string qqAccessTokenExpires: QQ AccessTokenExpires
	 * )
	 * 
	 * @return int 用户ID
	 * 
	 * @author zhuzhenguo
	 * Modified: 2012/12/09 23:59
	 * 1.添加注释，添加qq快捷登录三个参数
	 */
	public function addAccount($data) {
		$sql = '';
		
		$implode = array();
		
		if ( isset($data['email']) ) {
			$implode[] = "Email='" . $this->db->escape($data['email']) . "'";
		}
		else {
			return false;
		}
		if ( isset($data['password']) ) {
			$implode[] = "Password='" . sha1(md5($data['password'])) . "'";
		}
		else {
			return false;
		}
		if ( isset($data['alias']) ) {
			$implode[] = "Alias='" . $this->db->escape($data['alias']) . "'";
		}
		else {
			return false;
		}
		
		if ( isset($data['gender']) ) {
			$implode[] = "Gender='" . (int)($data['gender']) . "'";
		}
		if ( isset($data['birthday']) ) {
			$implode[] = "Birthday='" . $this->db->escape($data['birthday']) . "'";
		}
		if ( isset($data['birthdayVisible']) ) {
			$implode[] = "BirthdayVisible='" . $this->db->escape($data['birthdayVisible']) . "'";
		}
		if ( isset($data['constellation']) ) {
			$implode[] = "Constellation='" . (int)$data['constellation'] . "'";
		}
		if ( isset($data['constellationVisible']) ) {
			$implode[] = "ConstellationVisible='" . $this->db->escape($data['constellationVisible']) . "'";
		}
		if ( isset($data['bloodType']) ) {
			$implode[] = "BloodType='" . (int)$data['bloodType'] . "'";
		}
		if ( isset($data['bloodTypeVisible']) ) {
			$implode[] = "BloodTypeVisible='" . $this->db->escape($data['bloodTypeVisible']) . "'";
		}
		if ( isset($data['presentPlaceCountry']) ) {
			$implode[] = "PresentPlaceCountryID='" . (int)$data['presentPlaceCountry'] . "'";
		}
		if ( isset($data['presentPlaceProvince']) ) {
			$implode[] = "PresentPlaceProvinceID='" . (int)$data['presentPlaceProvince'] . "'";
		}
		if ( isset($data['presentPlaceCity']) ) {
			$implode[] = "PresentPlaceCityID='" . (int)$data['presentPlaceCity'] . "'";
		}
		if ( isset($data['presentPlaceDistrict']) ) {
			$implode[] = "PresentPlaceDistrictID='" . (int)$data['presentPlaceDistrict'] . "'";
		}
		if ( isset($data['presentPlaceVisible']) ) {
			$implode[] = "PresentVisible='" . $this->db->escape($data['presentPlaceVisible']) . "'";
		}
		if ( isset($data['homePlaceCountry']) ) {
			$implode[] = "HomePlaceCountryID='" . (int)$data['homePlaceCountry'] . "'";
		}
		if ( isset($data['homePlaceProvince']) ) {
			$implode[] = "HomePlaceProvinceID='" . (int)$data['homePlaceProvince'] . "'";
		}
		if ( isset($data['homePlaceCity']) ) {
			$implode[] = "HomePlaceCityID='" . (int)$data['homePlaceCity'] . "'";
		}
		if ( isset($data['homePlaceDistrict']) ) {
			$implode[] = "HomePlaceDistrictID='" . (int)$data['homePlaceDistrict'] . "'";
		}
		if ( isset($data['homePlaceVisible']) ) {
			$implode[] = "HomeVisible='" . $this->db->escape($data['homePlaceVisible']) . "'";
		}
		if ( isset($data['description']) ) {
			$implode[] = "Description='" . $this->db->escape($data['description']) . "'";
		}
		if ( isset($data['mobilePhone']) ) {
			$implode[] = "MobilePhone='" . $this->db->escape($data['mobilePhone']) . "'";
		}
		if ( isset($data['mobilePhoneVisible']) ) {
			$implode[] = "MobilePhoneVisible='" . $this->db->escape($data['mobilePhoneVisible']) . "'";
		}
		if ( isset($data['telephone']) ) {
			$implode[] = "Telephone='" . $this->db->escape($data['telephone']) . "'";
		}
		if ( isset($data['telephoneVisible']) ) {
			$implode[] = "TelephoneVisible='" . $this->db->escape($data['telephoneVisible']) . "'";
		}
		if ( isset($data['addressCountry']) ) {
			$implode[] = "AddressCountryID='" . (int)$data['addressCountry'] . "'";
		}
		if ( isset($data['addressProvince']) ) {
			$implode[] = "AddressProvinceID='" . (int)$data['addressProvince'] . "'";
		}
		if ( isset($data['addressCity']) ) {
			$implode[] = "AddressCityID='" . (int)$data['addressCity'] . "'";
		}
		if ( isset($data['addressDistrict']) ) {
			$implode[] = "AddressDistrictID='" . (int)$data['addressDistrict'] . "'";
		}
		if ( isset($data['address']) ) {
			$implode[] = "Address='" . $this->db->escape($data['address']) . "'";
		}
		if ( isset($data['addressVisible']) ) {
			$implode[] = "AddressVisible='" . $this->db->escape($data['addressVisible']) . "'";
		}
		if ( isset($data['zipCode']) ) {
			$implode[] = "ZipCode='" . $this->db->escape($data['zipCode']) . "'";
		}
		if ( isset($data['zipCodeVisible']) ) {
			$implode[] = "ZipCodeVisible='" . $this->db->escape($data['zipCodeVisible']) . "'";
		}
		if ( isset($data['contactEmail']) ) {
			$implode[] = "ContactEmail='" . $this->db->escape($data['contactEmail']) . "'";
		}
		if ( isset($data['contactEmailVisible']) ) {
			$implode[] = "ContactEmailVisible='" . $this->db->escape($data['contactEmailVisible']) . "'";
		}
		if ( isset($data['avatarID']) ) {
			$implode[] = "AvatarID='" . (int)$data['avatarID'] . "'";
		}
		if ( isset($data['imAccount']) ) {
			$implode[] = "IMAccount='" . $this->db->escape($data['imAccount']) . "'";
		}
		if ( isset($data['imAccountVisible']) ) {
			$implode[] = "IMAccountVisible='" . $this->db->escape($data['imAccountVisible']) . "'";
		}
		if ( isset($data['qqOpenid']) ) {
			$implode[] = "QQOpenid='" . $this->db->escape($data['qqOpenid']) . "'";
		}
		if ( isset($data['qqAccessToken']) ) {
			$implode[] = "QQAccessToken='" . $this->db->escape($data['qqAccessToken']) . "'";
		}
		if ( isset($data['qqAccessTokenExpires']) ) {
			$implode[] = "QQAccessTokenExpires='" . $this->db->escape($data['qqAccessTokenExpires']) . "'";
		}
		if ( isset($data['status']) ) {
			$implode[] = "Status='" . (int)$data['status'] . "'";
		}
		
		if ($implode) {
			$sql = ', ' . implode(", ", $implode);
		}
		
		$this->db->query("INSERT " . DB_PREFIX . "account SET JoinTime=NOW(), LastDatetime=NOW(), LastIP='" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "'" . $sql);
		
		return $this->db->getLastId();
	}
	
	public function addScore($account_id, $data) {	
		if (!isset($data['type']) || empty($data['type']) || !is_int($data['type'])) {
			return false;
		}
				
		if (!isset($data['amount']) || empty($data['amount']) || !is_int($data['amount'])) {
			return false;
		}
		
		if (!isset($data['voucher']) || is_null($data['voucher'])) {
			return false;
		}
		
		if (!isset($data['remark']) || is_null($data['remark'])) {
			return false;
		}
		
		$this->db->query("INSERT " . DB_PREFIX . "flow_score SET AccountID='" . (int)$account_id . "', AddDatetime=NOW(), Type='" . (int)$data['type'] . "', Amount='" . (int)$data['amount'] . "', Voucher='" . $this->db->escape($data['voucher']) . "', Remark='" . $this->db->escape($data['remark']) . "', SystemRemark=' REMOTE_ADDR: " . $this->db->escape($this->request->server['REMOTE_ADDR']) . "\n SERVER_ADDR: " . $this->db->escape($this->request->server['SERVER_ADDR']) . "\n SERVER_NAME: " . $this->db->escape($this->request->server['SERVER_NAME']) . "\n REQUEST_URI: " . $this->db->escape($this->request->server['REQUEST_URI']) . "\n HTTP_USER_AGENT: " . $this->db->escape($this->request->server['HTTP_USER_AGENT']) . "\n'");
		
		$query = $this->db->query("SELECT SUM(Amount) AS total FROM " . DB_PREFIX . "flow_score WHERE AccountID='" . (int)$account_id . "'");
		
		$this->db->query("UPDATE " . DB_PREFIX . "account SET Score='" . (int)$query->row['total'] . "' WHERE ID='" . (int)$account_id . "'");
		
		return true; 
	}
	
	public function addBalance($account_id, $data) {
		if (!isset($data['type']) || empty($data['type']) || !is_int($data['type'])) {
			return false;
		}
				
		if (!isset($data['amount']) || empty($data['amount']) || !is_int($data['amount'])) {
			return false;
		}
		
		if (!isset($data['voucher']) || is_null($data['voucher'])) {
			return false;
		}
		
		if (!isset($data['remark']) || is_null($data['remark'])) {
			return false;
		}
		
		$this->db->query("INSERT " . DB_PREFIX . "flow_fund SET AccountID='" . (int)$account_id . "', AddDatetime=NOW(), Type='" . (int)$data['type'] . "', Amount='" . (int)$data['amount'] . "', Voucher='" . $this->db->escape($data['voucher']) . "', Remark='" . $this->db->escape($data['remark']) . "', SystemRemark=' REMOTE_ADDR: " . $this->db->escape($this->request->server['REMOTE_ADDR']) . "\n SERVER_ADDR: " . $this->db->escape($this->request->server['SERVER_ADDR']) . "\n SERVER_NAME: " . $this->db->escape($this->request->server['SERVER_NAME']) . "\n REQUEST_URI: " . $this->db->escape($this->request->server['REQUEST_URI']) . "\n HTTP_USER_AGENT: " . $this->db->escape($this->request->server['HTTP_USER_AGENT']) . "\n'");
		
		$query = $this->db->query("SELECT SUM(Amount) AS total FROM " . DB_PREFIX . "flow_fund WHERE AccountID='" . (int)$account_id . "'");
		
		$this->db->query("UPDATE " . DB_PREFIX . "account SET Balance='" . (int)$query->row['total'] . "' WHERE ID='" . (int)$account_id . "'");
		
		return true; 
	}
	
	public function addActivityScore($account_id, $data) {
		if (!isset($data['type']) || empty($data['type']) || !is_int($data['type'])) {
			return false;
		}
		
		if (!isset($data['amount']) || empty($data['amount']) || !is_int($data['amount'])) {
			return false;
		}
		
		if (!isset($data['voucher']) || is_null($data['voucher'])) {
			return false;
		}
		
		if (!isset($data['remark']) || is_null($data['remark'])) {
			return false;
		}
		
		$this->db->query("INSERT " . DB_PREFIX . "flow_credit_activity SET AccountID='" . (int)$account_id . "', AddDatetime=NOW(), Type='" . (int)$data['type'] . "', Amount='" . (int)$data['amount'] . "', Voucher='" . $this->db->escape($data['voucher']) . "', Remark='" . $this->db->escape($data['remark']) . "', SystemRemark=' REMOTE_ADDR: " . $this->db->escape($this->request->server['REMOTE_ADDR']) . "\n SERVER_ADDR: " . $this->db->escape($this->request->server['SERVER_ADDR']) . "\n SERVER_NAME: " . $this->db->escape($this->request->server['SERVER_NAME']) . "\n REQUEST_URI: " . $this->db->escape($this->request->server['REQUEST_URI']) . "\n HTTP_USER_AGENT: " . $this->db->escape($this->request->server['HTTP_USER_AGENT']) . "\n'");
		
		$query = $this->db->query("SELECT SUM(Amount) AS total FROM " . DB_PREFIX . "flow_credit_activity WHERE AccountID='" . (int)$account_id . "' AND Amount>=0");
		
		$this->db->query("UPDATE " . DB_PREFIX . "account SET ActivityGood='" . (int)$query->row['total'] . "' WHERE ID='" . (int)$account_id . "'");
		
		$query = $this->db->query("SELECT SUM(Amount) AS total FROM " . DB_PREFIX . "flow_credit_activity WHERE AccountID='" . (int)$account_id . "' AND Amount<0");
		
		$this->db->query("UPDATE " . DB_PREFIX . "account SET ActivityBad='" . abs((int)$query->row['total']) . "' WHERE ID='" . (int)$account_id . "'");
		
		return true; 
	}
	
	public function addSponsorScore($account_id, $data) {
		if (!isset($data['type']) || empty($data['type']) || !is_int($data['type'])) {
			return false;
		}
		
		if (!isset($data['amount']) || empty($data['amount']) || !is_int($data['amount'])) {
			return false;
		}
		
		if (!isset($data['voucher']) || is_null($data['voucher'])) {
			return false;
		}
		
		if (!isset($data['remark']) || is_null($data['remark'])) {
			return false;
		}
		
		$this->db->query("INSERT " . DB_PREFIX . "flow_credit_sponsor SET AccountID='" . (int)$account_id . "', AddDatetime=NOW(), Type='" . (int)$data['type'] . "', Amount='" . (int)$data['amount'] . "', Voucher='" . $this->db->escape($data['voucher']) . "', Remark='" . $this->db->escape($data['remark']) . "', SystemRemark=' REMOTE_ADDR: " . $this->db->escape($this->request->server['REMOTE_ADDR']) . "\n SERVER_ADDR: " . $this->db->escape($this->request->server['SERVER_ADDR']) . "\n SERVER_NAME: " . $this->db->escape($this->request->server['SERVER_NAME']) . "\n REQUEST_URI: " . $this->db->escape($this->request->server['REQUEST_URI']) . "\n HTTP_USER_AGENT: " . $this->db->escape($this->request->server['HTTP_USER_AGENT']) . "\n'");
		
		$query = $this->db->query("SELECT SUM(Amount) AS total FROM " . DB_PREFIX . "flow_credit_sponsor WHERE AccountID='" . (int)$account_id . "' AND Amount>=0");
		
		$this->db->query("UPDATE " . DB_PREFIX . "account SET SponsorGood='" . (int)$query->row['total'] . "' WHERE ID='" . (int)$account_id . "'");
		
		$query = $this->db->query("SELECT SUM(Amount) AS total FROM " . DB_PREFIX . "flow_credit_sponsor WHERE AccountID='" . (int)$account_id . "' AND Amount<0");
		
		$this->db->query("UPDATE " . DB_PREFIX . "account SET SponsorBad='" . abs((int)$query->row['total']) . "' WHERE ID='" . (int)$account_id . "'");
		
		return true; 
	}
	
	public function editBase($data) {
		$sql = '';
		
		$implode = array();
		
		if (isset($data['gender']) && !is_null($data['gender'])) {
			$implode[] = "Gender='" . (int)($data['gender']) . "'";
		}
		if (isset($data['birthday']) && !is_null($data['birthday'])) {
			$implode[] = "Birthday='" . $this->db->escape($data['birthday']) . "'";
		}
		if (isset($data['birthdayVisible']) && !is_null($data['birthdayVisible'])) {
			$implode[] = "BirthdayVisible='" . $this->db->escape($data['birthdayVisible']) . "'";
		}
		if (isset($data['constellation']) && !is_null($data['constellation'])) {
			$implode[] = "Constellation='" . (int)$data['constellation'] . "'";
		}
		if (isset($data['constellationVisible']) && !is_null($data['constellationVisible'])) {
			$implode[] = "ConstellationVisible='" . $this->db->escape($data['constellationVisible']) . "'";
		}
		if (isset($data['bloodType']) && !is_null($data['bloodType'])) {
			$implode[] = "BloodType='" . (int)$data['bloodType'] . "'";
		}
		if (isset($data['bloodTypeVisible']) && !is_null($data['bloodTypeVisible'])) {
			$implode[] = "BloodTypeVisible='" . $this->db->escape($data['bloodTypeVisible']) . "'";
		}
		if (isset($data['presentPlaceCountry']) && !is_null($data['presentPlaceCountry'])) {
			$implode[] = "PresentPlaceCountryID='" . (int)$data['presentPlaceCountry'] . "'";
		}
		if (isset($data['presentPlaceProvince']) && !is_null($data['presentPlaceProvince'])) {
			$implode[] = "PresentPlaceProvinceID='" . (int)$data['presentPlaceProvince'] . "'";
		}
		if (isset($data['presentPlaceCity']) && !is_null($data['presentPlaceCity'])) {
			$implode[] = "PresentPlaceCityID='" . (int)$data['presentPlaceCity'] . "'";
		}
		if (isset($data['presentPlaceDistrict']) && !is_null($data['presentPlaceDistrict'])) {
			$implode[] = "PresentPlaceDistrictID='" . (int)$data['presentPlaceDistrict'] . "'";
		}
		if (isset($data['presentPlaceVisible']) && !is_null($data['presentPlaceVisible'])) {
			$implode[] = "PresentVisible='" . $this->db->escape($data['presentPlaceVisible']) . "'";
		}
		if (isset($data['homePlaceCountry']) && !is_null($data['homePlaceCountry'])) {
			$implode[] = "HomePlaceCountryID='" . (int)$data['homePlaceCountry'] . "'";
		}
		if (isset($data['homePlaceProvince']) && !is_null($data['homePlaceProvince'])) {
			$implode[] = "HomePlaceProvinceID='" . (int)$data['homePlaceProvince'] . "'";
		}
		if (isset($data['homePlaceCity']) && !is_null($data['homePlaceCity'])) {
			$implode[] = "HomePlaceCityID='" . (int)$data['homePlaceCity'] . "'";
		}
		if (isset($data['homePlaceDistrict']) && !is_null($data['homePlaceDistrict'])) {
			$implode[] = "HomePlaceDistrictID='" . (int)$data['homePlaceDistrict'] . "'";
		}
		if (isset($data['homePlaceVisible']) && !is_null($data['homePlaceVisible'])) {
			$implode[] = "HomeVisible='" . $this->db->escape($data['homePlaceVisible']) . "'";
		}
		if (isset($data['description']) && !is_null($data['description'])) {
			$implode[] = "Description='" . $this->db->escape($data['description']) . "'";
		}
		if (isset($data['qqOpenid'])) {
			$implode[] = "QQOpenid='" . $this->db->escape($data['qqOpenid']) . "'";
		}
		if (isset($data['qqAccessToken'])) {
			$implode[] = "QQAccessToken='" . $this->db->escape($data['qqAccessToken']) . "'";
		}
		if (isset($data['qqAccessTokenExpires'])) {
			$implode[] = "QQAccessTokenExpires='" . $this->db->escape($data['qqAccessTokenExpires']) . "'";
		}
		
		if ($implode) {
			$sql = implode(", ", $implode);
		}
		
		$this->db->query("UPDATE " . DB_PREFIX . "account SET " . $sql . " WHERE ID='" . (int)$this->member->getId() . "'");
		
		return true;
	}
	
	public function editContact($data) {
		$sql = '';
		
		$implode = array();
		
		if (isset($data['mobilePhone']) && !is_null($data['mobilePhone'])) {
			$implode[] = "MobilePhone='" . $this->db->escape($data['mobilePhone']) . "'";
		}
		if (isset($data['mobilePhoneVisible']) && !is_null($data['mobilePhoneVisible'])) {
			$implode[] = "MobilePhoneVisible='" . $this->db->escape($data['mobilePhoneVisible']) . "'";
		}
		if (isset($data['telephone']) && !is_null($data['telephone'])) {
			$implode[] = "Telephone='" . $this->db->escape($data['telephone']) . "'";
		}
		if (isset($data['telephoneVisible']) && !is_null($data['telephoneVisible'])) {
			$implode[] = "TelephoneVisible='" . $this->db->escape($data['telephoneVisible']) . "'";
		}
		if (isset($data['addressCountry']) && !is_null($data['addressCountry'])) {
			$implode[] = "AddressCountryID='" . (int)$data['addressCountry'] . "'";
		}
		if (isset($data['addressProvince']) && !is_null($data['addressProvince'])) {
			$implode[] = "AddressProvinceID='" . (int)$data['addressProvince'] . "'";
		}
		if (isset($data['addressCity']) && !is_null($data['addressCity'])) {
			$implode[] = "AddressCityID='" . (int)$data['addressCity'] . "'";
		}
		if (isset($data['addressDistrict']) && !is_null($data['addressDistrict'])) {
			$implode[] = "AddressDistrictID='" . (int)$data['addressDistrict'] . "'";
		}
		if (isset($data['address']) && !is_null($data['address'])) {
			$implode[] = "Address='" . $this->db->escape($data['address']) . "'";
		}
		if (isset($data['addressVisible']) && !is_null($data['addressVisible'])) {
			$implode[] = "AddressVisible='" . $this->db->escape($data['addressVisible']) . "'";
		}
		if (isset($data['zipCode']) && !is_null($data['zipCode'])) {
			$implode[] = "ZipCode='" . $this->db->escape($data['zipCode']) . "'";
		}
		if (isset($data['zipCodeVisible']) && !is_null($data['zipCodeVisible'])) {
			$implode[] = "ZipCodeVisible='" . $this->db->escape($data['zipCodeVisible']) . "'";
		}
		if (isset($data['contactEmail']) && !is_null($data['contactEmail'])) {
			$implode[] = "ContactEmail='" . $this->db->escape($data['contactEmail']) . "'";
		}
		if (isset($data['contactEmailVisible']) && !is_null($data['contactEmailVisible'])) {
			$implode[] = "ContactEmailVisible='" . $this->db->escape($data['contactEmailVisible']) . "'";
		}
		if (isset($data['imAccount']) && !is_null($data['imAccount'])) {
			$implode[] = "IMAccount='" . $this->db->escape($data['imAccount']) . "'";
		}
		if (isset($data['imAccountVisible']) && !is_null($data['imAccountVisible'])) {
			$implode[] = "IMAccountVisible='" . $this->db->escape($data['imAccountVisible']) . "'";
		}
		
		if ($implode) {
			$sql = implode(", ", $implode);
		}
		
		$this->db->query("UPDATE " . DB_PREFIX . "account SET " . $sql . " WHERE ID='" . (int)$this->member->getId() . "'");
		
		return true;
	}
	
	public function editAvatar($avatarID) {
		$this->db->query("UPDATE " . DB_PREFIX . "account SET AvatarID='" . (int)$avatarID . "' WHERE ID='" . (int)$this->member->getId() . "'");
		
		return true;
	}
	
	public function editPassword($password, $oldPassword) {
		$this->db->query("UPDATE " . DB_PREFIX . "account SET Password='" . sha1(md5($password)) . "' WHERE ID='" . (int)$this->member->getId() . "' AND Password='" .  sha1(md5($oldPassword)) . "'");
		
		return $this->db->countAffected();
	}
	/**
	 * 修改帐户状态
	 * 
	 * @param array $data(
	 * int *1 id : 用户ID,
	 * int *1 email : Email,
	 * int * status : 状态[1.正常，0.停用，2.待验证]
	 * )
	 * 
	 * @return int 影响数
	 * 
	 * @author zhuzhenguo 
	 * Modified: 2012/12/10 23:47
	 * 1.修改参数为$data, 2. 添加email条件参数
	 */
	public function editStatus($data) {
		$sql = '';
		$implode = array();
		
		if ( isset($data['id']) ) {
			$implode[] = "ID='" . (int)$data['id'] . "'";
		}
		
		if ( isset($data['email']) ) {
			$implode[] = "Email='" . $this->db->escape($data['email']) . "'";
		}
		
		if ( empty($implode) ) {
			return 0;
		}
		
		if ( !isset($data['status']) ) {
			return 0;
		}
		else {
			$sql = ' WHERE ' . implode(' AND ', $implode);
		}
		
		$this->db->query("UPDATE " . DB_PREFIX . "account SET Status='" . (int)$data['status'] . "'" . $sql);
		
		return $this->db->countAffected();
	}
}
?>